package com.compass.mobile.ui.fragment

import UserInfoManager
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentHospitalChoiceBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.constant.LOCAL_PHONE
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.constant.selfServicePhone
import com.compass.mobile.framework.constant.selfServiceWxCode
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.HospitalBean
import com.compass.mobile.presenters.HospitalPresenter
import com.compass.mobile.ui.activity.HomePageActivity
import com.compass.mobile.ui.viewModel.MainViewModel
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 选择医院
 * Date:2024/5/21 16:33
 */
class HospitalChoiceFragment : BaseVBFragment<FragmentHospitalChoiceBinding>() {
    private val mainViewModel: MainViewModel by activityViewModels()

    private var hospitals: MutableList<HospitalBean> = mutableListOf()
    override fun initView(view: View, savedInstanceState: Bundle?) {


    }


    override fun initData() {
        //这里后续接收动态的值
          UserInfoManager.putString(YD_USER_ID, "123123123315")//测试///
//        UserInfoManager.putString(YD_USER_ID, "123123123313")//马
//        UserInfoManager.putString(YD_USER_ID, "123123123321")//移18718729311990动/薛雄飞
//        UserInfoManager.putString(YD_USER_ID, "12312345")//测试


        showLoading()
        mainViewModel.getHospitalList()
        mainViewModel.loginLiveData.observe(this) {
            dismissLoading()
            it?.let {
                UserInfoManager.saveAccessToken(it.accessToken)
                UserInfoManager.saveRefreshToken(it.refreshToken)
                HomePageActivity.start(requireContext())
            } ?: kotlin.run {
                showToast("用户信息异常")
            }
        }
        mainViewModel.hospitalListLiveData.observe(this) {
            dismissLoading()
            it?.let { list ->
                hospitals = list
                specialColumn()
            }
        }
        mainViewModel.userLiveData.observe(this){
            it?.let {
                UserInfoManager.putString(YD_USER_ID, it.id)
                UserInfoManager.putString(LOCAL_PHONE, it.mobile)
                mainViewModel.boxLogin(it.id)
            }

        }
    }

    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {

            showLoading()
            UserInfoManager.saveHospitalId(hospitals[position].id)
            UserInfoManager.saveHospitalCode(hospitals[position].code)
            UserInfoManager.putString(selfServiceWxCode,hospitals[position].selfServiceWxCode)
            UserInfoManager.putString(selfServicePhone,hospitals[position].selfServicePhone)
            UserInfoManager.savePlatformId(hospitals[position].platformId)
           //后续需要接收移动传入的token 在这里切换
            mainViewModel.boxLogin(UserInfoManager.getString(YD_USER_ID))
//            mainViewModel.getUserPhoneForToken("3f98becde6d24ed7ba0e133ca558e5f9")


        }
    }

    @SuppressLint("RestrictedApi")
    fun specialColumn() {
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)
        val arrayObjectAdapter = ArrayObjectAdapter(HospitalPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )
        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        arrayObjectAdapter.addAll(0, hospitals)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )
        mBinding?.caseItem?.setSelectedPositionSmooth(0)

    }



}